package com.hyt.it.ogt.kq.service.bm.api;

import com.alibaba.fastjson.JSON;
import com.hyt.it.ogt.kq.service.bm.feign.tps.TpsClient;
import com.hyt.it.ogt.kq.service.bm.service.IUserPermissionService;
import com.hyt.loginfo.controller.BaseLoginController;
import com.hyt.loginfo.service.IBaseLoginService;
import com.hyt.model.QuotEditor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘奇
 * @date 2020/7/9
 * @Description
 */
public abstract class ApiBaseController extends BaseLoginController {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
	private IBaseLoginService iBaseLoginService;
    
    
    @Autowired
	private TpsClient tpsClient;
    
    @Autowired
	private IUserPermissionService iUserPermissionService;
    
    /**
     * 获取登录用户ID
     * @return String
     */
    @Override
    public String getUserId() {
    	return iBaseLoginService.getUserId();
    }
    
    /**
     * 获取机构ID
     * @return String
     */
    @Override
    public String getOfficeId() {
    	return iBaseLoginService.getOfficeId();
    }
    
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(String.class, new QuotEditor()); //双引号特殊处理
    }
    
    
    
    @SuppressWarnings("unchecked")
	public String getBmProjectQrCodeUrl(String officeId) {
		Map<String, Object> map = new HashMap<String, Object>();
		String qrCodeUr= null;
		if(StringUtils.isEmpty(officeId)) {
			return qrCodeUr;
		}
		try {
			String scene = "offId=";
			if (officeId.length() > 24) {
				scene = scene + officeId.substring(0,24);
			} else {
				scene = scene + officeId;
			}
			map = (Map<String, Object>) tpsClient.getBmQRCodeHwCloud(officeId, scene, "pages/home/index", 260);
			logger.info("getBmQRCodeHwCloud scene:{},result:{}", scene, JSON.toJSON(map));
		} catch (Exception e) {
			logger.error("## getBmQRCodeHwCloud error", e);
		}
		if(null != map) {
			qrCodeUr = String.valueOf(map.get("obj"));
		}
		return qrCodeUr;
	}
    
    public  List<String> getPermissionList(String projectId) {
    	List<String> permissionList = new ArrayList<String>();
        boolean flag = iUserPermissionService.needCheckAreaPermission(projectId);
        if (flag) {
            permissionList = iUserPermissionService.queryPermissionByUserId(projectId, iBaseLoginService.getUserId());
            if (permissionList != null && permissionList.size() == 0) {
                permissionList = new ArrayList<String>();
                permissionList.add("NoPermission");//如果是无权限，设置一个不存在的权限
            }
        }
		return permissionList;
    }
    
}
